package com.moody.init;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Map.Entry;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.moody.hibernate.domain.CodeLib;

/*
 * Copyright ? Moody analytics   
 *
 * @Title: Debt.java 
 * @Package： com.moody.init 
 * @author： Liu Peng
 * @Description: 债项管理的下拉列表数据
 * @date： 2012-3-7 下午12:29:05
 * @Modified by: Liu Peng 
 * @version V1.0   
 */

public class Debt {
	private static final Logger log = LoggerFactory
	.getLogger(DataDictionary.class);
	private static Debt instance = null;
	private Map<String, List<CodeLib>> forwardMap;//key——大类code ; value——小类对象的链表
	private Map<String, CodeLib> reverseMap; //key——小类code  ;  value——大类对象
	
	private Debt() {
		initMap();
	}
	
	private void initMap(){
		forwardMap = new HashMap<String,List<CodeLib>>();
		reverseMap = new HashMap<String, CodeLib>();
		Map<String, CodeLib> DebtType = DataDictionary.getInstance().getDataDictionary().get("DebtType");
		Iterator<String> it = DebtType.keySet().iterator();
		List<CodeLib> list = new LinkedList<CodeLib>();//存储编码不唯一的大类，如：个人按揭贷款
		while(it.hasNext()){
			String itemNo = it.next();
			CodeLib item = DebtType.get(itemNo);
			if(itemNo.length() == 4){//大类
				if(item.getItemDesc().length() > 1) {//编码不唯一的大类：ItemDesc存储该大类对应的子类前缀
					list.add(item); 
				}
			}
			
			else if(itemNo.length() == 8){//小类
				String prefix = itemNo.substring(0,4);//所属大类的code
				if(item.getItemDesc().length() > 1){// 前缀特殊的小类：ItemDesc指定所属大类，而不是通过前缀来判断
					prefix = item.getItemDesc();
				}
				if(forwardMap.containsKey(prefix))forwardMap.get(prefix).add(item);
				else{
					List<CodeLib> itemList = new LinkedList<CodeLib>();
					itemList.add(item);
					forwardMap.put(prefix, itemList);
				}
				reverseMap.put(itemNo,DebtType.get(prefix));
			}
		}
		for(CodeLib levelOne : list){
			String levelOneItemNo = levelOne.getId().getItemNo();
			String prefixes = levelOne.getItemDesc();
			StringTokenizer st = new StringTokenizer(prefixes,",");
			while(st.hasMoreTokens()){
				String prefix = st.nextToken();
				if(prefix.equals(levelOneItemNo))continue;//将自己code作为主键，将其他的主键对应的list添加到自己code上
				else if(forwardMap.containsKey(prefix) && forwardMap.containsKey(levelOneItemNo)){
					forwardMap.get(levelOneItemNo).addAll(forwardMap.get(prefix));
					forwardMap.remove(prefix);
					reverseMap.put(prefix, levelOne);
				}
			}
		}
		it = DebtType.keySet().iterator();
		while(it.hasNext()){
			String itemNo = it.next();
			CodeLib item = DebtType.get(itemNo);
			if(itemNo.length() == 8){
				String prefix = itemNo.substring(0,4);//所属大类的code
				if(item.getItemDesc().length() > 1){// 前缀特殊的小类：ItemDesc指定所属大类，而不是通过前缀来判断
					prefix = item.getItemDesc();
				}
				reverseMap.put(itemNo,DebtType.get(prefix) == null? DebtType.get(reverseMap.get(prefix).getId().getItemNo()):DebtType.get(prefix));
			}
		}
	}

	public static Debt getInstance() {
		if (instance == null) {
			instance = new Debt();
		}
		return instance;
	}
	public List<Map<String,String>> getDebtTypeLevelOne(){
		Map<String, CodeLib> DebtType = DataDictionary.getInstance().getDataDictionary().get("DebtType");
		Iterator<String> it = forwardMap.keySet().iterator();
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		while(it.hasNext()){
			String key = it.next();
			Map<String,String> map = new HashMap<String,String>();
			map.put("code", key);
			map.put("name", DebtType.get(key).getItemName());
			list.add(map);
		}
		return list;
	}
	
	public List<Map<String,String>> getDebtTypeLevelTwo(String levelOneCode){
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		if(levelOneCode == null || levelOneCode.equals("") || forwardMap.get(levelOneCode) == null) return list;
		Iterator<CodeLib> it = forwardMap.get(levelOneCode).iterator();
		while(it.hasNext()){
			CodeLib item = it.next();
			Map<String,String> map = new HashMap<String,String>();
			map.put("code", item.getId().getItemNo());
			map.put("name", item.getItemName());
			list.add(map);
		}
		return list;
	}
	
	public String getDebtTypeLevelOneCodeByLevelTwoCode(String levelTwoCode){
		return reverseMap.get(levelTwoCode).getId().getItemNo();
	}
	
	
	
	/** 以下方法用于 InvalidDebtType的情况：
	public List<Map<String,String>> getDebtTypeLevelOne(){
		Map<String, CodeLib> DebtType = DataDictionary.getInstance().getDataDictionary().get("DebtType");
		
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		
		Map<String, String> map1 = new LinkedHashMap<String, String>();
		map1.put("code", "01");
		map1.put("name", DebtType.get("01").getItemName());
		list.add(map1);
		Map<String, String> map2 = new LinkedHashMap<String, String>();
		map2.put("code", "02");
		map2.put("name", DebtType.get("02").getItemName());
		list.add(map2);
		Map<String, String> map3 = new LinkedHashMap<String, String>();
		map3.put("code", "03");
		map3.put("name", DebtType.get("03").getItemName());
		list.add(map3);
		Map<String, String> map4 = new LinkedHashMap<String, String>();
		map4.put("code", "04");
		map4.put("name", DebtType.get("04").getItemName());
		list.add(map4);
		Map<String, String> map5 = new LinkedHashMap<String, String>();
		map5.put("code", "05");
		map5.put("name", DebtType.get("05").getItemName());
		list.add(map5);
		Map<String, String> map6 = new LinkedHashMap<String, String>();
		map6.put("code", "06");
		map6.put("name", DebtType.get("06").getItemName());
		list.add(map6);
		Map<String, String> map7 = new LinkedHashMap<String, String>();
		map7.put("code", "07");
		map7.put("name", DebtType.get("07").getItemName());
		list.add(map7);
		Map<String, String> map8 = new LinkedHashMap<String, String>();
		map8.put("code", "08");
		map8.put("name", DebtType.get("08").getItemName());
		list.add(map8);
		Map<String, String> map9 = new LinkedHashMap<String, String>();
		map9.put("code", "09");
		map9.put("name", DebtType.get("09").getItemName());
		list.add(map9);
		Map<String, String> map10 = new LinkedHashMap<String, String>();
		map10.put("code", "10");
		map10.put("name", DebtType.get("10").getItemName());
		list.add(map10);
		Map<String, String> map11 = new LinkedHashMap<String, String>();
		map11.put("code", "11");
		map11.put("name", DebtType.get("11").getItemName());
		list.add(map11);
		Map<String, String> map12 = new LinkedHashMap<String, String>();
		map12.put("code", "12");
		map12.put("name", DebtType.get("12").getItemName());
		list.add(map12);
		Map<String, String> map13 = new LinkedHashMap<String, String>();
		map13.put("code", "13");
		map13.put("name", DebtType.get("13").getItemName());
		list.add(map13);
		Map<String, String> map14 = new LinkedHashMap<String, String>();
		map14.put("code", "14");
		map14.put("name", DebtType.get("14").getItemName());
		list.add(map14);
		Map<String, String> map15 = new LinkedHashMap<String, String>();
		map15.put("code", "15");
		map15.put("name", DebtType.get("15").getItemName());
		list.add(map15);
		Map<String, String> map16 = new LinkedHashMap<String, String>();
		map16.put("code", "16");
		map16.put("name", DebtType.get("16").getItemName());
		list.add(map16);
		Map<String, String> map17 = new LinkedHashMap<String, String>();
		map17.put("code", "17");
		map17.put("name", DebtType.get("17").getItemName());
		list.add(map17);
		Map<String, String> map20 = new LinkedHashMap<String, String>();
		map20.put("code", "20");
		map20.put("name", DebtType.get("20").getItemName());
		list.add(map20);
		Map<String, String> map40 = new LinkedHashMap<String, String>();
		map40.put("code", "40");
		map40.put("name", DebtType.get("40").getItemName());
		list.add(map40);
		
		return list;
	}
	
	public Map getDebtTypeLevelTwo(){
		Map<String, CodeLib> DebtType = new LinkedHashMap<String, CodeLib>();
		DebtType = DataDictionary.getInstance().getDataDictionary().get("DebtType");
		Map map = new LinkedHashMap();
		
		Iterator iter =  DebtType.entrySet().iterator();
		
		while (iter.hasNext()) { 
			Map.Entry entry = (Map.Entry) iter.next(); 
			String code = (String) entry.getKey(); 
		    String name = ((CodeLib)entry.getValue()).getItemName();
		    
		    if(code.length() == 4){
		    	String firstLevelCode = code.substring(0, 2);
		    	if(!map.containsKey(firstLevelCode)){
		    		List<Map<String, String>> secondLevel = new ArrayList<Map<String, String>>();
		    		Map<String, String> secondLevelMap = new LinkedHashMap<String, String>();
		    		secondLevelMap.put("code", code);
		    		secondLevelMap.put("name", name);
		    		secondLevel.add(secondLevelMap);
			    	map.put(firstLevelCode, secondLevel);
		    	}
		    	else{
		    		Map<String, String> secondLevelMap = new LinkedHashMap<String, String>();
		    		secondLevelMap.put("code", code);
		    		secondLevelMap.put("name", name);
		    		((List)map.get(firstLevelCode)).add(secondLevelMap);
		    	}
		    }
		}
		return map;
	}
	
*/
	public List getDebtType(){
		Map<String, CodeLib> DebtType = new LinkedHashMap<String, CodeLib>();
		DebtType = DataDictionary.getInstance().getDataDictionary().get("DebtType");
		List list = new LinkedList<Map<String,String>>();
		
		Iterator iter =  DebtType.entrySet().iterator();
		while(iter.hasNext()){
			Map.Entry entry = (Map.Entry) iter.next(); 
			String code = (String) entry.getKey(); 
		    String name = ((CodeLib)entry.getValue()).getItemName();
		    Map<String,String> itemMap = new LinkedHashMap<String,String>();
		    itemMap.put("code", code);
		    itemMap.put("name", name);
		    list.add(itemMap);
		}
		return list;
	}
}
